﻿Public Class Frm_a7kam
    Public Sub initializeControls()
        If Comb_type.SelectedIndex = 1 Then
            pret.Enabled = True
            Comb_kholassa.Enabled = True
            Comb_kholassa.SelectedIndex = 0
        Else
            Comb_kholassa.SelectedIndex = -1
            pret.Enabled = False
            Comb_kholassa.Enabled = False
            pret.Checked = False
        End If
    End Sub
    Private Sub Btn_parc_marji3ona_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Rech_Doss.Click
        Me.Enabled = False
        Partager.frmafect = Me
        Frm_Recherche.Show()
        Partager.type = Me.Name
    End Sub

    Private Sub Btn_rech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_rech.Click
        If Comb_marji3ona.SelectedIndex = -1 Then
            MsgBox("اختر المرجع أولا", MsgBoxStyle.Critical, "خطأ في البحث")
        Else
            Me.Enabled = False
            Frm_Rech_a7kam.Show()
        End If
    End Sub

   
    Public dv As Dossier

    Private Sub Frm_a7kam_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Partager.nonInitial = New ArrayList
            Partager.nonInitial.Add("Comb_marji3ona")
            Partager.remplirDossier(Comb_marji3ona)
            '--------------------------------------
            Dim i As Integer = 0
            If Partager.idDossier <> 0 Then
                For Each item In Comb_marji3ona.Items
                    Partager.deClt = DirectCast(item, DictionaryEntry)
                    If CInt(Partager.deClt.Key) = Partager.idDossier Then
                        Comb_marji3ona.SelectedIndex = i
                    End If
                    i += 1
                Next
                Partager.idDossier = 0
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Comb_marji3ona_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_marji3ona.SelectedIndexChanged
        Try
            Partager.vider(Me, Partager.nonInitial)
            initializeControls()

            Partager.deMarji = DirectCast(Comb_marji3ona.SelectedItem, DictionaryEntry)

                For Each d As Dossier In Partager.moi.List_info_fichier
                    If d.id = CInt(Partager.deMarji.Key) Then
                        dv = d
                        dv.remplir_List_decisions()
                        Exit For
                    End If
                Next
           

            List_tous.Items.Clear()
            If Not dv Is Nothing Then
                For Each khib As decisions In dv.List_decisions
                    List_tous.Items.Add(New ListViewItem(New String() {khib.Num_decision, khib.Date_decision.ToString("dd/MM/yyy"), khib.Type, khib.Resum, khib.Declamation}))
                Next
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Public Function Unique7okm(ByVal Num_Decision As String, Optional ByVal id As Integer = 0) As Boolean
        Dim bol As Boolean = True
        For Each dos As decisions In dv.List_decisions
            If id <> 0 Then
                If id = dos.Id Then
                    Continue For
                End If
            End If

            If (dos.Num_decision = Num_Decision) Then
                bol = False
                Exit For
            End If
        Next
        Return bol
    End Function

    Public avo As decisions
    Private Sub Btn_ajout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout.Click
        Try
            If Comb_marji3ona.SelectedIndex >= 0 Then
                If (List_tous.SelectedIndices.Count > 0) Then
                    MsgBox("عفوا لا يمكنك الإضافة، لإضافة حكم أو قرار جديد عليك الضغط على الزر جديد ثم إدخال البيانات", MsgBoxStyle.Critical, "خطأ في الإضافة")
                Else
                    If txt_num_9arar.Text <> "" Then
                        If Unique7okm(txt_num_9arar.Text) Then
                            If MsgBox("هل تريد فعلا إضافة هذاالحكم أو القرار؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة حكم أو قرار") = MsgBoxResult.Yes Then
                                Partager.close()
                                Dim cli As New decisions(1, txt_num_9arar.Text, Date_9arar.Value, Comb_type.Text, Comb_kholassa.Text, txt_mantou9.Text, txt_molahadat.Text, pret.Checked)
                                dv.Ajouter_decisions(cli)
                                List_tous.Items.Clear()
                                For Each khib As decisions In dv.List_decisions
                                    List_tous.Items.Add(New ListViewItem(New String() {khib.Num_decision, khib.Date_decision.ToString("dd/MM/yyy"), khib.Type, khib.Resum, khib.Declamation}))
                                Next
                                Partager.vider(Me, Partager.nonInitial)
                                MsgBox("تم الإضافة بنجاح", MsgBoxStyle.Information, "نجاح")
                            End If
                        Else
                            MsgBox("هذا الرقم مستعمل من قبل", MsgBoxStyle.Critical, "خطأ")
                        End If
                    Else
                        MsgBox("لم تدخل رقم الحكم", MsgBoxStyle.Critical, "خطأ")
                    End If
                End If
            Else
                MsgBox("اختر الملف أولا", MsgBoxStyle.Critical, "خطأ")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_modif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_modif.Click
        Try
            If Comb_marji3ona.SelectedIndex >= 0 Then
                If (List_tous.SelectedIndices.Count <= 0) Then
                    MsgBox("عفوا لا يمكنك التعديل، اختر حكم أو قرار من القائمة", MsgBoxStyle.Critical, "خطأ في التعديل")
                Else

                    If txt_num_9arar.Text <> "" Then
                        If Unique7okm(txt_num_9arar.Text, avo.Id) Then
                            If MsgBox("هل تريد فعلا تعديل هذا الحكم أو القرار؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "تعديل حكم أو قرار") = MsgBoxResult.Yes Then
                                Partager.close()
                                Dim cli As New decisions(1, txt_num_9arar.Text, Date_9arar.Value, Comb_type.Text, Comb_kholassa.Text, txt_mantou9.Text, txt_molahadat.Text, pret.Checked)
                                dv.modifier_decisions(cli, avo.Id)
                                List_tous.Items.Clear()
                                For Each khib As decisions In dv.List_decisions
                                    List_tous.Items.Add(New ListViewItem(New String() {khib.Num_decision, khib.Date_decision.ToString("dd/MM/yyy"), khib.Type, khib.Resum, khib.Declamation}))
                                Next
                                Partager.vider(Me, Partager.nonInitial)
                                MsgBox("تمت عملية التعديل بنجاح", MsgBoxStyle.Information, "نجاح")
                            End If
                        Else
                            MsgBox("هذا الرقم مستعمل من قبل", MsgBoxStyle.Critical, "خطأ")
                        End If
                    Else
                        MsgBox("لم تدخل رقم الحكم", MsgBoxStyle.Critical, "خطأ")
                    End If
                End If
            Else
                MsgBox("اختر الملف أولا", MsgBoxStyle.Critical, "خطأ")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click
        If List_tous.SelectedIndices.Count > 0 Then
            If MsgBox("هل تريد فعلا حذف هذا الحكم أو القرار ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف") = MsgBoxResult.Yes Then
                Partager.close()
                Try
                    Dim clt As decisions = dv.List_decisions(List_tous.SelectedIndices.Item(0))
                    dv.suprimmer_decisions(avo.Id)

                    List_tous.Items.Clear()
                    For Each khib As decisions In dv.List_decisions
                        List_tous.Items.Add(New ListViewItem(New String() {khib.Num_decision, khib.Date_decision.ToString("dd/MM/yyy"), khib.Type, khib.Resum, khib.Declamation}))
                    Next
                    Partager.vider(Me, Partager.nonInitial)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Else
            MsgBox("اختر حكم أو قرار من القائمة", MsgBoxStyle.Information, "خطأ")
        End If
    End Sub

    Private Sub List_tous_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous.SelectedIndexChanged
        If List_tous.SelectedIndices.Count > 0 Then
            Try
                For Each i As Integer In List_tous.SelectedIndices
                    avo = dv.List_decisions(i)

                    txt_num_9arar.Text = avo.Num_decision
                    Date_9arar.Value = avo.Date_decision
                    Comb_type.Text = avo.Type
                    Comb_kholassa.Text = avo.Resum
                    txt_mantou9.Text = avo.Declamation
                    txt_molahadat.Text = avo.Remarque
                    If avo.pret Then
                        pret.Checked = True
                    Else
                        pret.Checked = False
                    End If
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        Else
            Partager.vider(Me, Partager.nonInitial)
        End If
    End Sub

    Private Sub Comb_type_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_type.SelectedIndexChanged
         initializeControls()
    End Sub

    Private Sub Btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_nouv.Click
        Partager.vider(Me, Partager.nonInitial)
    End Sub

    Private Sub Btn_ToTabligh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ToTabligh.Click
        If List_tous.SelectedIndices.Count > 0 Then
            If avo.Type = "قطعي" Then
                Frm_Tabligh.MdiParent = Frm_Main
                Me.Enabled = False
                Frm_Tabligh.Show()
            Else
                MsgBox("هذا الحكم ليس قطعي", MsgBoxStyle.Information, "خطأ")
            End If
        Else
            MsgBox("اختر حكم أو قرار من القائمة", MsgBoxStyle.Information, "خطأ")
        End If
    End Sub

    Private Sub Btn_ToTanfid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ToTanfid.Click
        If List_tous.SelectedIndices.Count > 0 Then
            If avo.Type = "قطعي" Then
                Frm_Execution.MdiParent = Frm_Main
                Me.Enabled = False
                Frm_Execution.Show()
            Else
                MsgBox("هذا الحكم ليس قطعي", MsgBoxStyle.Information, "خطأ")
            End If
        Else
            MsgBox("اختر حكم أو قرار من القائمة", MsgBoxStyle.Information, "خطأ")
        End If
    End Sub
    Private Sub txt_num_9arar_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_num_9arar.KeyPress
        Dim allowedChars As String = "1234567890/"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub

End Class